<script>
import bmdForm from "@/components/DmbForm";
import dmbTable from "@/components/LTable/index.vue";
import {
  defaultForm,
  formBase,
  tableBase,
  cellStyle,
  headerCellStyle,
  tableFormStyle,
  formItemStyle,
} from "./config";
import {
  warehouseManagement_DelAPI,
  warehouseManagement_ImportAPI,
} from "@/api/data/warehouse-management";
import { warehouseType_ListApi } from "@/api/data/warehouse-type.js";
import { mapState } from "vuex";
import dayjs from "dayjs";
export default {
  name: "warehouseManagement",
  computed: {
    ...mapState({
      destination: (state) => state.custom_dict.destination,
    }),
    headerCellStyle() {
      return headerCellStyle;
    },
    tableFormStyle() {
      return tableFormStyle;
    },
    cellStyle() {
      return cellStyle;
    },
    formItemStyle() {
      return formItemStyle;
    },
  },
  components: { bmdForm, dmbTable },
  data() {
    return {
      propsSearchForm: { ...defaultForm },
      requestConfig: {
        url: "/base/manage/warehouse/list",
        method: "get",
        montage: true,
      },
      saveRequest: {
        url: "/base/manage/warehouse/save",
        method: "post",
        montage: false,
      },
      selectionArr: [], // 多选框内容
      inputfile: null,
      tableBase,
    };
  },

  methods: {
    async del(id) {
      await this.$confirm("确定要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      });
      await warehouseManagement_DelAPI(id);
      this.$refs.table.getListData();
      this.$message.success("删除成功");
    },
    formBase() {
      return formBase;
    },
    // 导出
    downLoad() {
      if (this.selectionArr.length > 0) {
        const ids = this.selectionArr.map((item) => {
          return item.id;
        });

        this.download(
          "/base/manage/warehouse/exportWarehouse",
          {
            ids,
          },
          `仓库管理_${dayjs().format("YYYY/MM/DD hh:mm:ss")}.xlsx`
        );
      } else {
        this.$message.error("请选择条数据导出");
      }
    },
    // 导入
    importFile() {
      this.$refs.inputfile.click();
    },
    changeExcel(e) {
      const that = this;
      // 错误情况判断
      const files = e.target.files[0];
      console.log(files);
      if (files.length <= 0) {
        return false;
      }
      let data = new FormData();
      data.append("file", files);
      warehouseManagement_ImportAPI(data).then((res) => {
        this.$message.success("导入成功");
        this.$refs.table.getListData();
      });
    },
    // 回显字典相应数据
    selectLabel(data, id) {
      const result = data.find((item) => {
        return item.id === id;
      });
      return result ? result.destinationName : "";
    },
  },
  mounted() {
    warehouseType_ListApi({ pageSize: 999999999, pageNum: 1 }).then((res) => {
      console.log(res, 1);
      const obj = this.tableBase.find((v) => v.label === "仓库类型");
      obj.operate.options = res.rows;
    });
  },
};
</script>

<template>
  <div>
    <div class="mb-3">
      <bmd-form
        ref="form"
        :form-items="formBase()"
        :model-value="propsSearchForm"
        :item-style="formItemStyle"
        @update="(row) => (propsSearchForm = row)"
      >
        <template v-slot:footer="row" class="flex flex-row-reverse">
          <div class="mb-3 flex flex-row-reverse">
            <div>
              <el-button type="success" @click="$refs.table.getListData()"
                >查询</el-button
              >
              <el-button type="success" @click="$refs.table.saveToUpdate()"
                >保存</el-button
              >
              <el-button type="success" @click="downLoad">导出</el-button>
              <el-button type="primary" @click="importFile">导入</el-button>
              <!-- 不显示 文件上传的图标 -->
              <input
                type="file"
                accept=".xls,.xlsx"
                ref="inputfile"
                @change="changeExcel"
                class="hidden"
              />
            </div>
          </div>
        </template>
      </bmd-form>
    </div>
    <div>
      <dmbTable
        ref="table"
        :operateWether="true"
        :prop-list="tableBase"
        :is-operate="true"
        :show-index-column="true"
        select-column-type="CHECKBOX"
        :props-search-form="propsSearchForm"
        :req-request="requestConfig"
        :saveRequest="saveRequest"
        :isSaveToUpadate="true"
        select-key="id"
        :cell-style="cellStyle"
        :header-cell-style="headerCellStyle"
        :initialize="false"
        :change-search-form="false"
        :show-footer="true"
        :form-item-style="tableFormStyle"
        @selectionChange="
          (row) => {
            selectionArr = row;
          }
        "
      >
        <template v-slot:operate="{ row }">
          <el-button type="danger" v-if="row.id" @click="del(row.id)"
            >删除</el-button
          >
        </template>
      </dmbTable>
    </div>
  </div>
</template>

<style scoped lang="scss">
</style>
